'use strict';
angular
	.module('services')
	.service(
		'PaginationService',
		[
			'$http',
			function($http) {

			    var pagination = {
				data : [],
				max : 5,
				offset : 0,
				currentPage : 1,
				maxPage : 0,
				count : 0,
				controller : "",
				action : "",
				parameters : "",
				sort : "",

				getPageNumbers : function() {
				    return pagesNumber();
				},
				getNextPage : function() {
				    return next();
				},
				getPreviousPage : function() {
				    return previous();
				},
				getPage : function(i) {
				    return page(i);
				}
			    };

			    var data = function(controller, action, parameters,
				    sort) {
				pagination.controller = controller;
				pagination.action = action;
				pagination.parameters = parameters;
				pagination.sort = sort;
				loadData();
				return pagination.data;
			    };

			    var loadData = function() {
				$http.jsonp(
					'http://localhost:8090/CarManagement/'
						+ pagination.controller + '/'
						+ pagination.action + '?'
						+ pagination.parameters
						+ '&max=' + pagination.max
						+ '&offset='
						+ pagination.offset + '&sort='
						+ pagination.sort
						+ '&callback=JSON_CALLBACK')
					.success(function(data) {
					    pagination.data = data;
					}).error(
						function(data, status, headers,
							config) {
						    pagination.data = [
							    "Echec !", status,
							    headers, config ];
						});
				$http.jsonp(
					'http://localhost:8090/CarManagement/'
						+ pagination.controller
						+ '/count?'
						+ pagination.parameters
						+ '&callback=JSON_CALLBACK')
					.success(function(data) {
					    pagination.count = data.count;
					    pagination.maxPage = getMaxPage();
					});
			    };

			    var next = function() {
				var nextPage = pagination.currentPage + 1;
				if (nextPage <= pagination.maxPage) {
				    var newOffset = pagination.offset
					    + pagination.max;
				    pagination.offset = newOffset;
				    pagination.currentPage++;
				    $http
					    .jsonp(
						    'http://localhost:8090/CarManagement/'
							    + pagination.controller
							    + '/'
							    + pagination.action
							    + '?'
							    + pagination.parameters
							    + '&max='
							    + pagination.max
							    + '&offset='
							    + pagination.offset
							    + '&callback=JSON_CALLBACK')
					    .success(function(data) {
						pagination.data = data;
					    })
					    .error(
						    function(data, status,
							    headers, config) {
							pagination.data = [
								"Echec !",
								status,
								headers, config ];
						    });
				}
			    };

			    var page = function(pageNumber) {
				pagination.offset = pagination.max
					* (pageNumber - 1);
				pagination.currentPage = pageNumber;
				$http.jsonp(
					'http://localhost:8090/CarManagement/'
						+ pagination.controller + '/'
						+ pagination.action + '?'
						+ pagination.parameters
						+ '&max=' + pagination.max
						+ '&offset='
						+ pagination.offset
						+ '&callback=JSON_CALLBACK')
					.success(function(data) {
					    pagination.data = data;
					}).error(
						function(data, status, headers,
							config) {
						    pagination.data = [
							    "Echec !", status,
							    headers, config ];
						});
			    };

			    var previous = function() {
				var newOffset = pagination.offset
					- pagination.max;
				if (newOffset >= 0) {
				    pagination.offset = newOffset;
				    pagination.currentPage--;
				    $http
					    .jsonp(
						    'http://localhost:8090/CarManagement/'
							    + pagination.controller
							    + '/'
							    + pagination.action
							    + '?'
							    + pagination.parameters
							    + '&max='
							    + pagination.max
							    + '&offset='
							    + pagination.offset
							    + '&callback=JSON_CALLBACK')
					    .success(function(data) {
						pagination.data = data;
					    })
					    .error(
						    function(data, status,
							    headers, config) {
							pagination.data = [
								"Echec !",
								status,
								headers, config ];
						    });
				}
			    };

			    var getMaxPage = function() {
				var div = pagination.count / pagination.max;
				var rem = pagination.count % pagination.max;
				var max = Math.floor(div) + rem;
				return max;
			    };

			    var pagesNumber = function() {
				var result = [];
				for ( var i = 1; i <= pagination.maxPage; i++) {
				    result.push(i);
				}
				return result;
			    };

			    return {
				initData : function(controller, action,
					parameters, sort) {
				    return data(controller, action, parameters,
					    sort);
				},
				getPagination : function() {
				    return pagination;
				}
			    };

			} ]);
